package Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.newsapplication.MainActivity;
import com.example.newsapplication.R;

import java.io.File;
import java.io.FileInputStream;

public class LoginActivity extends AppCompatActivity {
    private Button check_user;
    private SQLiteOpenHelper dbHelper;
    private EditText username,password;
    private ImageView login_head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        check_user = (Button) findViewById(R.id.check_user);
        username = (EditText) findViewById(R.id.login_username);
        password = (EditText) findViewById(R.id.login_password);
        login_head = (ImageView) findViewById(R.id.login_head);

        initRoundedDrawable();

        check_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SQLiteDatabase db = dbHelper.getReadableDatabase();
//                String username_str = username.getText().toString();
//                String userpassword_str = password.getText().toString();
//
//                Cursor cursor = db.rawQuery("select * from User where name=?",new String[]{username_str});   //遍历数据库查找数据
//                if(cursor.getCount()==0){
//                    Toast.makeText(LoginActivity.this,"用户名不存在！",Toast.LENGTH_SHORT).show();
//                }else{
//                    if(cursor.moveToFirst()){
//                        String password_db = cursor.getString(cursor.getColumnIndex("password"));
//                        if(userpassword_str.equals(password_db)){
//                            Toast.makeText(LoginActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(LoginActivity.this,"密码错误！",Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }
//                cursor.close();
//                db.close();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });


    }
    //头像圆角设置
    private void initRoundedDrawable(){
        ImageView view = findViewById(R.id.login_head);
        Bitmap src = BitmapFactory.decodeResource(getResources(),R.drawable.head);
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),src);
        roundedBitmapDrawable.setCornerRadius(2500);
        roundedBitmapDrawable.setAntiAlias(true); // 设置抗锯齿
        view.setImageDrawable(roundedBitmapDrawable); // 显示圆角
    }
}